package com.newlink.building.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import com.alibaba.android.arouter.launcher.ARouter
import com.newlink.building.activity.NL_LicensePopupActivityNL
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.constant.Base_Constant
import com.newlink.building.common_base.constant.Base_RouterPath
import com.newlink.building.common_base.utils.Base_Preference
import com.newlink.building.databinding.ActivitySplashBinding
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker

/**
 * Created by xiaodong on 2022/7/25.
 */
@SuppressLint("CustomSplashScreen")
class NL_SplashActivityNL : NL_BaseActivity() {

    private lateinit var binding: ActivitySplashBinding

    // 用于判断是否是首次启动应用
    private var isAppFirstLaunch: Boolean by Base_Preference(Base_Constant.APP_FIRST_LOAD, true)

    // 动画对象，延迟初始化
    private var splashFadeAnimation: Animation? = null

    override fun attachLayoutRes(): View {
        binding = ActivitySplashBinding.inflate(layoutInflater)
        return binding.root
    }

    /**
     * 初始化界面元素，包括动画配置与启动
     */
    override fun initView() {
        // 启动画面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("SplashActivity", "initView")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom(System.currentTimeMillis())

        setupFadeAnimation()
        startFadeAnimation()
    }

    /**
     * 初始化数据逻辑
     */
    override fun initData() {
        if (isAppFirstLaunch) {
            navigateToLicenseAndFinish()
        }
    }

    /**
     * 设置淡入动画参数
     */
    private fun setupFadeAnimation() {
        splashFadeAnimation = AlphaAnimation(0.3F, 1F).apply {
            duration = 2000
            setAnimationListener(createAnimationEndListener())
        }
    }

    /**
     * 启动动画
     */
    private fun startFadeAnimation() {
        binding.clSplash.startAnimation(splashFadeAnimation)
    }

    /**
     * 创建动画监听器，用于动画结束时跳转主界面
     */
    private fun createAnimationEndListener(): Animation.AnimationListener {
        return object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {}

            override fun onAnimationEnd(animation: Animation?) {
                jumpToMainPage()
            }

            override fun onAnimationRepeat(animation: Animation?) {}
        }
    }

    /**
     * 跳转到主页或登录页
     */
    private fun jumpToMainPage() {
        // 页面跳转前的混淆
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(7, isLogin)
        Module_Obfuscation_ObfuscationHelper.beforeNetworkRequest(Base_RouterPath.PATH_HOME, mapOf("source" to "splash"))

        val routePath = if (isLogin) Base_RouterPath.PATH_HOME else Base_RouterPath.PATH_LOGIN
        ARouter.getInstance().build(routePath).navigation()

        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        finish()
    }

    /**
     * 如果是首次启动，则跳转授权页面并关闭当前页面
     */
    private fun navigateToLicenseAndFinish() {
        startActivity(Intent(this, NL_LicensePopupActivityNL::class.java))
        finish()
    }

    override fun start() {
        // 空实现，预留扩展
    }

    override fun onDestroy() {
        super.onDestroy()
        splashFadeAnimation?.cancel()
        splashFadeAnimation = null
    }
}
